package com.woniuxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.dao.DriverMapper;
import com.woniuxy.pojo.Driver;
import com.woniuxy.service.DriverService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.vo.DriverVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.DriverManager;
import java.util.List;

@Service
public class DriverServiceImpl implements DriverService {
    @Autowired
    private DriverMapper driverMapper;

    @Override
    public DataGridView getDriverListByPage(DriverVo driverVo) {
        Page<Object> page=PageHelper.startPage(driverVo.getPage(),driverVo.getLimit());
        List<Driver> driverList=this.driverMapper.selectDriverByProperty(driverVo);
        return new DataGridView(page.getTotal(),driverList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addDriver(Driver driver) {
        boolean flag=false;
        int count=this.driverMapper.insertDriver(driver);
        if(count>0){
            flag=true;
        }
        return flag;
    }

    @Override
    public Driver findDriverByPhone(String phone) {
        return this.driverMapper.findDriverByPhone(phone);
    }

    @Transactional
    @Override
    public void removeDriver(int id) {
        this.driverMapper.deleteByPrimaryKey(id);
    }
    @Transactional
    @Override
    public void removeBathDriver(Integer[] ids) {
        if(ids.length>0){
            for(Integer id:ids){
                this.removeDriver(id);
            }
        }
    }

    @Override
    public boolean update(Driver driver) {
        int res = driverMapper.update(driver);
        if(res>0){
            return true;
        }
        return false;
    }
}
